package com.shujia.mr.pcount;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class PcountReducer extends Reducer<Text, LongWritable, Text, LongWritable> {
    @Override
    protected void reduce(Text key, Iterable<LongWritable> values, Reducer<Text, LongWritable, Text, LongWritable>.Context context) throws IOException, InterruptedException {
        long count = 0L;
        long sum = 0L;
        for (LongWritable value : values) {
            sum += value.get();
            count++;
        }

        //求每一个人的平均消费
        long avg_counts = sum / count;
        context.write(key, new LongWritable(avg_counts));
    }
}
